//
//  SlideBntView.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/1/26.
//  Copyright © 2016年 game. All rights reserved.
//

#import "SlideBntView.h"
#import "Masonry.h"

@interface SlideBntView ()
@property (nonatomic,strong) UIImageView *downimg;
@property (nonatomic,strong) UILabel *changeLab;

@property (nonatomic,strong) UIImageView *ToImageview;

@property (nonatomic,assign) BOOL isClick;

@end

@implementation SlideBntView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = RGBA(242, 242, 242, 1);
        self.changeLab.text = @"切换栏目";
        self.titleLabel.text = @"首页";
        
        self.downimg.backgroundColor = [UIColor clearColor];
        [self.synBnt setTitle:@"删除选择" forState:UIControlStateNormal];
        
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
        
        self.ToImageview.backgroundColor = [UIColor clearColor];
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapClick)];
        
        [self addGestureRecognizer:tap];
    }
    return self;
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = font(SCREEN_WIDTH*0.05);
        [self addSubview:_titleLabel];
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@15);
            make.centerY.equalTo(self);
        }];
    }
    return _titleLabel;
}

/**
 *  首页两个字后面的 图片
 */
-(UIImageView *)ToImageview{
    if (!_ToImageview) {
    
        _ToImageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"enter"]];
        
        [self addSubview:_ToImageview];
        
        [_ToImageview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLabel.mas_right).offset(5);
            make.centerY.equalTo(self).offset(1);
            make.size.equalTo(MASBoxValue(CGSizeMake(10, 16)));
        }];
    }

    return _ToImageview;
}


-(UILabel *)selecLab{
    if (!_selecLab) {
        _selecLab = [UILabel new];
        
        _selecLab.textAlignment = NSTextAlignmentLeft;
        _selecLab.font = font(SCREEN_WIDTH*0.05);
        [self addSubview:_selecLab];
        
        [_selecLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_ToImageview.mas_right).offset(5);
            make.centerY.equalTo(self);
        }];
    }
    return _selecLab;
}

-(UILabel *)changeLab{

    if (!_changeLab) {
        _changeLab = [UILabel new];
        _changeLab.font = font(SCREEN_WIDTH*0.05);
        _changeLab.textAlignment = NSTextAlignmentLeft;
        _changeLab.alpha = 0;
        [self addSubview:_changeLab];
        
        [_changeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@15);
            make.centerY.equalTo(self);
            
        }];

    }
    return _changeLab;

}

-(UIImageView *)downimg{
    if (!_downimg) {
        _downimg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"btn-down"]];
        [self addSubview:_downimg];
        
        [_downimg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@-10);
            make.centerY.equalTo(self);
            make.size.equalTo(MASBoxValue(CGSizeMake(20, 20)));
            
        }];
    }

    return _downimg;
}

-(UIButton *)synBnt{
    if (!_synBnt) {
        _synBnt = [UIButton buttonWithType:UIButtonTypeSystem];
        _synBnt.alpha = 0;
        _synBnt.layer.borderColor = mainColor.CGColor;
        _synBnt.layer.borderWidth = 1;
        [_synBnt setTitleColor:mainColor forState:UIControlStateNormal];
        _synBnt.titleLabel.font = font(SCREEN_WIDTH*0.04);
        [_synBnt addTarget:self action:@selector(SynClick) forControlEvents:UIControlEventTouchUpInside];
        radius(_synBnt, 10);
        
        [self addSubview:_synBnt];
        
        [_synBnt mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_downimg.mas_left).offset(-10);
            make.centerY.top.bottom.equalTo(_downimg);
            make.width.equalTo(@70);
        }];
    }

    return _synBnt;
}

-(void)TapClick{

     [self AnimationWhithClick];
}

-(void)AnimationWhithClick{
    
    self.isClick = !self.isClick;
    if (self.isClick) {
        
        self.titleLabel.alpha = 0;
        self.selecLab.alpha = 0;
        self.ToImageview.alpha = 0;

        [UIView animateWithDuration:0.5 animations:^{
            [Manager makeRotation:_downimg speedX:0 speedY:0.5];
            
            [self.deleget TapDown];//展开
            
            self.synBnt.alpha = 1;
            
            self.changeLab.alpha = 1;
        }];

    }else{
        
         self.changeLab.alpha = 0;
        
        [UIView animateWithDuration:0.5 animations:^{
            [Manager makeRotation:_downimg speedX:0 speedY:-0.5];
            
            [self.deleget TapTopPut];//收回
            
            self.synBnt.alpha = 0;
            self.titleLabel.alpha = 1;
            self.selecLab.alpha = 1;
            self.ToImageview.alpha = 1;
        }];
        
    }
}

-(void)SynClick{
    self.titleLabel.text = @"首页";
}
@end
